package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadImgController {

    @RequestMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(MultipartFile pic) throws Exception {
        // 务必要使用json格式数据
        return UploadUtil.uploadAli(pic);
    }


    @RequestMapping("/uploadImg_ck")
    @ResponseBody
    public Object uploadImgByCk(MultipartFile upload) throws Exception {

        Map<String,Object> map = new HashMap<>();
        // 上传文件的全民(加上后缀: 例如hot3.png)
        String filename =  upload.getOriginalFilename();
        String url = null;

        if (upload != null && upload.getSize() > 0){
            try {
                url = UploadUtil.uploadAli(upload);
                map.put("uploaded", 1);
                map.put("filename", filename);
                map.put("url", url);
            } catch (Exception e) {
                e.printStackTrace();
                map.put("uploaded",0);
                Map<String,Object> error = new HashMap<>();
                error.put("msg", e.getMessage());
                map.put("error", error);
            }
        }
        return map;
    }
}
